package com.lancoo.tyjx.multichatroom.xmpp;

import android.content.Intent;
import android.util.Log;

import com.lancoo.tyjx.multichatroom.activity.MyApplication;
import com.lancoo.tyjx.multichatroom.constants.Constants;
import com.lancoo.tyjx.multichatroom.db.MsgDbHelper;
import com.lancoo.tyjx.multichatroom.model.ChatItem;
import com.lancoo.tyjx.multichatroom.utils.DateUtil;
import com.lancoo.tyjx.multichatroom.utils.FileUtil;
import com.lancoo.tyjx.multichatroom.utils.JsonUtil;

import org.jivesoftware.smack.PacketInterceptor;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;


public class XmppMessageInterceptor implements PacketInterceptor {

    @Override
    public void interceptPacket(Packet packet) {
        Message nowMessage = (Message) packet;
        if (Constants.IS_DEBUG)
            Log.e("xmppchat send", nowMessage.toXML());
        if (nowMessage.getType() == Message.Type.groupchat
                || nowMessage.getType() == Message.Type.chat) {
            String chatName = "";
            String userName = "";
            int chatType = ChatItem.CHAT;
            //name
            if (nowMessage.getType() == Message.Type.groupchat) {
                chatName = XmppTool.getRoomName(nowMessage.getTo());
                userName = nowMessage.getTo();
                chatType = ChatItem.GROUP_CHAT;
            } else {
                chatName = userName = XmppTool.getUsername(nowMessage.getTo());
            }
            //type
            // 记录我们发出去的消息
            String msgBody;
            int audiotime = 0;
//			String msgContent=JsonUtil.json2XmppMsg(nowMessage.getBody());
            String msgType = JsonUtil.whatMsgType(nowMessage.getBody());
            String msgContent = JsonUtil.json2XmppMsg(nowMessage.getBody());//消息实际内容
            String msgHeaderImg = JsonUtil.json2XmppMsgHeaderImg(nowMessage.getBody());// 获取头像地址
            String msgSendTime = JsonUtil.getSendTime(nowMessage.getBody());

            if (msgType.equalsIgnoreCase("like")) {
                if (msgContent.equalsIgnoreCase("like"))
                    msgBody = "对本堂课{点赞}";
                else
                    msgBody = "对本堂课{取消点赞}";
            } else if (nowMessage.getProperty("imgData") != null) {
                if (FileUtil.getType(msgContent) == FileUtil.SOUND)
                    msgBody = Constants.SAVE_SOUND_PATH + "/" + msgContent;
                else
                    msgBody = Constants.SAVE_IMG_PATH + "/" + msgContent;
            } else if (nowMessage.getType() == Message.Type.groupchat
                    & msgContent.contains(":::")) { //被迫的
                String[] msgAndData = msgContent.split(":::");
                if (FileUtil.getType(msgAndData[0]) == FileUtil.SOUND) {
                    msgBody = Constants.SAVE_SOUND_PATH + "/" + msgAndData[0];
                    audiotime = JsonUtil.getAudioTime(nowMessage.getBody());
                } else {
                    msgBody = Constants.SAVE_IMG_PATH + "/" + msgAndData[0];
                }
            } else {
                msgBody = msgContent;
            }
            if (msgContent.contains("[RoomChange")) {
                System.out.println("房间要发生改变了");
            } else {
                if (!msgType.equalsIgnoreCase("like") &&
                        !msgType.equalsIgnoreCase("unlike")) {//自己发的点赞消息不显示在消息列表中
                    ChatItem msg = new ChatItem(chatType, chatName, userName, msgHeaderImg, msgBody, msgSendTime, 1, 1);
                    msg.setMsgType(msgType);
                    msg.setVoicetime(audiotime);
                    MsgDbHelper.getInstance(MyApplication.getInstance()).saveChatMsg(msg);
                    MyApplication.getInstance().sendBroadcast(new Intent("ChatNewMsg"));
                }
            }
        }
    }
}
